/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.ContactGroup;

/**
 *
 * @author hbriand
 * 
 * @class ContactGroupListPanel
 * @desc The list of contact groups
 */
public class ContactGroupListPanel extends JPanel{

    /**
	 * 
	 */
	private static final long serialVersionUID = -3200713763439236714L;
	private ArrayList<ContactGroup> list;
    private JList contactGroupsList;
    private ContactGroupListDelegate delegate;

    public ContactGroupListPanel() {
        this.list = new ArrayList<ContactGroup>();
        this.redraw();
    }

    public void setGroupContactList(ArrayList<ContactGroup> groupList) {
        this.list = groupList;
        this.redraw();
    }

    public void redraw() {
        this.removeAll();
        this.setSize(266, 600);
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));


        // Add button to add a contact group
        JButton addBtn = new JButton("Add Group");
        addBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                delegate.addContactGroup();
            }
        });
        this.add(addBtn);

        //Contact groups list
        contactGroupsList = new JList();
        contactGroupsList.setFixedCellHeight(20);
        contactGroupsList.setFixedCellWidth(233);
        contactGroupsList.setVisibleRowCount(20);
        @SuppressWarnings("serial")
		DefaultListModel lm = new DefaultListModel() {};
        for(ContactGroup cg : this.list) {
            lm.addElement(cg.getName());
        }
        contactGroupsList.setModel(lm);
        contactGroupsList.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent lse) {
                if (delegate != null) {
                    System.out.println("Changed value"+list.get(contactGroupsList.getSelectedIndex()).getName());
                    if(contactGroupsList.getSelectedIndex() != 0)
                    	delegate.contactGroupSelected(list.get(contactGroupsList.getSelectedIndex()));
                    else
                    	delegate.contactDefaultGroupSelected();
                } else {
                    System.err.println("Delegate is not set");
                }
            }
        });
        JScrollPane jsp = new JScrollPane(contactGroupsList);
        this.add(jsp);


        //Action buttons
        JButton editBtn = new JButton("Edit");
        editBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                if (contactGroupsList.getSelectedIndex()>0)
                    delegate.editContactGroup(list.get(contactGroupsList.getSelectedIndex()));
            }
        });
        this.add(editBtn);

        JButton delBtn = new JButton("Delete");
        delBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                if (contactGroupsList.getSelectedIndex()>0)
                    delegate.deleteContactGroup(list.get(contactGroupsList.getSelectedIndex()));
            }
        });
        this.add(delBtn);

    }

    public void setDelegate(ContactGroupListDelegate aThis) {
        this.delegate = aThis;
    }

    public ContactGroup getCurrentSelectedGroup() {
        return list.get(contactGroupsList.getSelectedIndex());
    }

}
